package com.zj.demospringboot2.redisandjpa.controller;

import com.zj.demospringboot2.redisandjpa.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class RedisTestController {

    @Autowired
    private RedisService redisService;

    /**
     * 设置Redis数据
     *
     * @param key
     * @param value
     * @return
     */
    @PostMapping("/set")
    public Object setRedis(String key, String value) {
        redisService.set(key, value);
        return "设置成功";
    }

    /**
     * 根据key删除Redis数据
     *
     * @param key
     * @return
     */
    @GetMapping("/del")
    public Object delRedis(String key) {
        redisService.remove(key);
        return "删除成功";
    }

    /**
     * 根据key获取Redis数据
     *
     * @param key
     * @return
     */
    @GetMapping("/get")
    public Object getRedis(String key) {
        Object value = redisService.get(key);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("value", value);
        return map;
    }


}
